home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 20 / Cream of the Crop 20 (Terry Blount) (1996).iso / program / n_b_v203.zip / FGETKEY.TXT < prev    next >
Text File  |  1996-07-04  |  7KB  |  154 lines

  1. $if 0
  2.     ┌──────────────────────────╖                        PowerBASIC v3.20
  3.  ┌──┤          DASoft          ╟──────────────────────┬──────────────────╖
  4.  │  ├──────────────────────────╢    Copyright 1995    │ DATE: 1996-03-08 ╟─╖
  5.  │  │ FILE NAME   FGETKEY .TXT ║          by          ╘════════════════─ ║ ║
  6.  │  │                          ║  Don Schullian, Jr.                     ║ ║
  7.  │  ╘══════════════════════════╝                                         ║ ║
  8.  │ A license is hereby granted to the holder to use this source code in  ║ ║
  9.  │ any program, commercial or otherwise,  without receiving the express  ║ ║
  10.  │ permission of the copyright holder and without paying any royalties,  ║ ║
  11.  │ as long as this code is not distributed in any compilable format.     ║ ║
  12.  │  IE: source code files, PowerBASIC Unit files, and printed listings   ║ ║
  13.  ╘═╤═════════════════════════════════════════════════════════════════════╝ ║
  14.    │                ....................................                   ║
  15.    ╘═══════════════════════════════════════════════════════════════════════╝
  16. $endif
  17.  
  18. '.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°.°
  19. ' ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° ° °
  20.  
  21. Hi,
  22.  
  23.   Hopefully, in this text, I will be able to endoctrinate you to the use of
  24. a small function called fGetKey%. It does NOT exist in the library but there
  25. are several importable versions of it in the help screens and you will see
  26. various versions of it throughout the .DMO files.
  27.  
  28.   In its simple form it looks like this:
  29.  
  30. FUNCTION fGetKey% () LOCAL PUBLIC
  31.  
  32.   WHILE NOT INSTAT : WEND
  33.   FUNCTION = CVI( INKEY$ + CHR$(0) )
  34.  
  35. END FUNCTION
  36.  
  37. All it is doing here is awaiting a key-presss, converting it to an INTEGER
  38. value and returning that value. So, what's all the hubbub about? Well, as
  39. I said, this is the "basic, stripped down version". Other versions can
  40. trap incoming keys, branch the program to other tasks (like pop-up help),
  41. detect no keyboard action for a number of seconds and automatically call
  42. the blank-screen routine, or just about anything else you can think of
  43. including mouse tracks!
  44.  
  45. The most important thing is does, however, is not in the function's code.
  46. It provides/forces all incoming input from the keyboard and/or mouse to
  47. pass through one, centeralized location in the program. Every routine or
  48. function MUST come through here to get a key. Once you know where things
  49. are happening it is an easy matter of taking some action on it.
  50.  
  51. Two simple examples come to mind: For years I have used the pipe "|" as
  52. a data seperater in my programs. Because I need to keep this character
  53. out of my users' data I can't let them input from the keyboard. Try doing
  54. that if you are using INKEY$ in 30 different locations! Or, I once had a
  55. program that required the use of the degree sign "°" that is NOT accessible
  56. from the keyboard except by using <ALT>2<ALT>4<ALT>8. The user was not using
  57. the pipe (because I was blocking it) so every time they pressed "|" they got
  58. "°" and it all took one line of code for the whole program!
  59.  
  60. FUNCTION fGetKey% () LOCAL PUBLIC
  61.   LOCAL G%
  62.  
  63.   WHILE NOT INSTAT : WEND
  64.   G% = CVI( INKEY$ + CHR$(0) )
  65.   IF G% = 124 THEN G% = 248        ' "|" = "°"
  66.   FUNCTION = G%
  67.  
  68. END FUNCTION
  69.  
  70.   What this all means is greater control over your program. It also allows
  71. you to do EXACTLY the same thing as ON KEY(n) GOTO without generating 2
  72. extra bytes of code for each command! In a large program that can be 40 or
  73. 50 thousand bytes! You can even to a passable replacement for ON TIMER if
  74. it's not critical. (Most on-screen clocks aren't)
  75.  
  76.   So, now you understand, hopefully, the why and we can get onto the "what"
  77. of fGetKey%.  Every since I started programming I've used G$ to carry the
  78. incoming key-strokes. Then, a few years back I started using the ASCII value
  79. and finally I'm into this method which is better than anything I've seen
  80. before since it is almost foolproof! Here are some of the values for those
  81. "special" keys around the keyboard:
  82.  
  83. %ESC_key  =  27             ' or CVI( CHR$(027,000) )
  84. %F01_key  = (256 * 059)     ' or CVI( CHR$(000,059) )
  85. %F02_key  = (256 * 060)     ' or CVI( CHR$(000,060) )
  86. %F03_key  = (256 * 061)     ' or CVI( CHR$(000,061) )
  87. %F04_key  = (256 * 062)     ' or CVI( CHR$(000,062) )
  88. %F05_key  = (256 * 063)     ' or CVI( CHR$(000,063) )
  89.  
  90. When you press F1 INKEY$ inputs CHR$(000,059); when you press ESC it sends
  91. back a single CHR$(27) so (look back at any fGetKey% here) if you take the
  92. incoming key-press, add a trailing CHR$(0), convert the whole thing to an
  93. INTEGER using CVI you end up with an almost unique value for every key or
  94. combination of keys on the keyboard. ( %CTRL_L_BRACKET and %ESC_key both
  95. equal 27, etc. ) What this means in your programs is that because we are
  96. using INTEGERs instead of strings less memory is in use, faster access to
  97. the information is obtained and easier/faster tests are made on the keys.
  98.  
  99. G% = fGetKey%
  100. SELECT CASE G%
  101.   CASE 32 TO 255          ' a regular key
  102.   CASE %ESC_key           ' see how easy it is to read when you
  103.   CASE %F01_key           ' use the %constants?
  104.   CASE %SHIFT_F1          '
  105.   CASE %F10_key           '
  106.   CASE %ALT_X             '
  107.   CASE %ENTER_key
  108.   CASE %CTRL_GREY5
  109. END SELECT
  110.  
  111. This all brings us down to KEYCODES.INC which is a file that contains 170
  112. constants for the values of the "special" keys on the keyboard. There are
  113. actually 2 of these files, one is in this directory and one is in the
  114. directory with the .PBLs. The difference is that the one in this directory
  115. is/was made for human consumption while the one with the .PBLs (the one to
  116. include) is more PowerBASIC compatible.
  117.  
  118.      humanized              Better for PowerBASIC
  119. ────────────────────────────────────────────────────
  120. %F01_key  = (256 * 059)  :  %F01_key = 15104
  121. %F02_key  = (256 * 060)  :  %F02_key = 15360
  122. %F03_key  = (256 * 061)  :  %F03_key = 15616
  123. %F04_key  = (256 * 062)  :  %F04_key = 15872
  124. %F05_key  = (256 * 063)  :  %F05_key = 16128
  125.  
  126. When I'm working on a unit file that will be compiled for a .PBU I don't
  127. include the whole file but only pick-out the ones to be used. This, then
  128. provides me a quick reference of which keys are used by the routines all
  129. neatly listed at the top of the code. %F01_key is also a lot easier to
  130. read in the code than CHR$(000,059) too!
  131.  
  132. Now, have a look at FGETKEY.DMO, KEYCODES.INC (both) and everything should
  133. snap into place (if it hasn't already:)
  134.  
  135. Happy computing!
  136.  
  137. d83)
  138.  
  139. The following routines use key-press values like those returned by fGetKey%
  140.  
  141. fGetKey%     ' get one from the keyboard buffer
  142. fAnyKey%     ' pause program until a key-press (with ClearKeyboard)
  143. fPutKey%     ' stuff one back into the keyboard buffer
  144. fExitKey%    ' test if a key-press is present in a string of key-presses
  145. fEventKey%   ' keyboard/mouse driver
  146. fKeySelect%  ' menu driver
  147. fTinput%     ' field/data input routine
  148. fTinputM%    ' field/data input routine Mouse aware
  149. fTmenuDD?    ' drop-down/pull-down menu
  150. fTmenuHOT%   ' box menus
  151. fTmenuINDEX  ' index menu
  152. fYesNo?      ' yes/no dialogue box driver
  153.  
  154.   ---- And ANY routine/function that gathers data from the keyboard. ----